package com.jwthhealth_pub.old.diet.view.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.common.ImageLoader;
import com.jwthhealth_pub.old.diet.moudel.DietSecModule;
import com.jwthhealth_pub.old.diet.presenter.IDietPresneterComple;
import com.jwthhealth_pub.old.diet.view.DietSecActivity;
import com.jwthhealth_pub.old.diet.view.DietThiActivity;

import java.util.List;

/**
 * Created by Administrator on 2016/12/27.
 */
public class DietSecAdapter extends RecyclerView.Adapter {

    public static int JUMP = 0;
    public static int TOAS = 1;

    private Context mContext;
    private String topId;
    private String id;
    private List<DietSecModule.DataBean> data;
    private ProgressBar progressBar;
    private boolean loading;

    public void setLoading(boolean loading) {
        this.loading = loading;
    }

    public void setProgressBar(ProgressBar progressBar) {
        this.progressBar = progressBar;
    }

    private final IDietPresneterComple iDietPresneterComple;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Intent intent = new Intent(mContext, DietThiActivity.class);
                    Bundle data = msg.getData();
                    intent.putExtras(data);
                    mContext.startActivity(intent);
                    progressBar.setVisibility(View.GONE);
                    loading = false;
                    break;
                case 1:
                    Toast.makeText(mContext, msg.getData().getString(Constant.DIETDATA), Toast.LENGTH_SHORT).show();
                    progressBar.setVisibility(View.GONE);
                    loading = false;
                    break;
                default:break;
            }
        }
    };

    public DietSecAdapter(String topId, String id, DietSecActivity dietSecActivity, List<DietSecModule.DataBean> data) {
        this.data = data;
        this.mContext = dietSecActivity;
        this.topId = topId;
        this.id = id;
        iDietPresneterComple = new IDietPresneterComple(mContext);
        iDietPresneterComple.setHandler(handler);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHoler holder = new ViewHoler(LayoutInflater.from(mContext).inflate(R.layout.item_diet_sec, null));
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHoler viewHoler = (ViewHoler) holder;

        final DietSecModule.DataBean dataBean =  data.get(position);
        // TODO: 2016/12/29 莫名的出现空指针
        if (dataBean == null || dataBean.getTypename() == null) {
            return;
        }
        viewHoler.title.setText(dataBean.getTypename());
        viewHoler.secTitle.setText(dataBean.getName());
//        Picasso.with(mContext).load(dataBean.getPic_path()).into(viewHoler.img);
        ImageLoader.picasso(dataBean.getPic_path(), viewHoler.img);
        viewHoler.summary.setText(dataBean.getShort_desc());

        viewHoler.clickLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (loading) {
                    return;
                }
                iDietPresneterComple.requestThiInfo(topId, id ,dataBean.getType(), "1", dataBean.getTypename());
                loading = true;
                progressBar.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class ViewHoler extends RecyclerView.ViewHolder{

        TextView title, secTitle, summary;
        ImageView img;
        RelativeLayout clickLayout;

        public ViewHoler(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.item_title);
            secTitle = (TextView) itemView.findViewById(R.id.item_sec_title);
            summary = (TextView) itemView.findViewById(R.id.item_summary);
            img = (ImageView) itemView.findViewById(R.id.item_img);
            clickLayout = (RelativeLayout) itemView.findViewById(R.id.click_layout);
        }
    }

}
